#ifndef __KEY_DRV_H
#define __KEY_DRV_H

#include "main.h"

typedef struct Key_GPIO
{
    gpio_type *gpio;
    uint16_t pin;
    uint8_t ActiveLevel; /* 按键电平 */
} Key_GPIO_t;

static const Key_GPIO_t g_gpioList[] = {
    {.gpio = GPIOB, .pin = GPIO_PINS_7, .ActiveLevel = 0}, /* key2 */    
    {.gpio = GPIOB, .pin = GPIO_PINS_5, .ActiveLevel = 0}, /* key0 */
    {.gpio = GPIOB, .pin = GPIO_PINS_6, .ActiveLevel = 0}, /* key1 */
};

#define KEY_NUM_MAX (sizeof(g_gpioList) / sizeof(g_gpioList[0]))

typedef enum
{
    KEY_RELEASE = 0, /* 释放松开 */
    KEY_CONFIRM,     /* 消抖确认 */
    KEY_SHORTPRESS,  /* 短按 */
    KEY_LONGPRESS,   /* 长按 */
} KEY_STATE;

#define CONFIRM_TIME 10                /* 按键消抖时间间隔10ms */
#define DOUBLE_CLICK_INTERVAL_TIME 300 /* 两次抬起时间间隔300ms，用来判断是否双击 */
#define LONGPRESS_TIME 1000            /* 长按时间间隔1000ms */

typedef struct
{
    KEY_STATE keyState;            /* 按键状态 */
    uint8_t singleClickNum;        /* 按键单击次数 */
    uint64_t firstIoChangeSysTime; /* 按键第一次按下时间 */
    uint64_t firstReleaseSysTime;  /* 按键第一次抬起时间 */
} Key_Info_t;

#define KEY_NONE 0x00          /* 无按键 */
#define KEY1_SHORT_PRESS 0x01  /* KEY1短按码值 0x01 */
#define KEY1_DOUBLE_PRESS 0x51 /* KEY1双击码值 0x51 */
#define KEY1_LONG_PRESS 0x81   /* KEY1长按码值 0x81 */
#define KEY2_SHORT_PRESS 0x02  /* KEY2短按码值 0x02 */
#define KEY2_DOUBLE_PRESS 0x52 /* KEY2双击码值 0x52 */
#define KEY2_LONG_PRESS 0x82   /* KEY2长按码值 0x82 */
#define KEY3_SHORT_PRESS 0x03  /* KEY3短按码值 0x03 */
#define KEY3_DOUBLE_PRESS 0x53 /* KEY3双击码值 0x53 */
#define KEY3_LONG_PRESS 0x83   /* KEY3长按码值 0x83 */

void KeyDrvInit(void);
uint8_t GetKeyVal(void);
void KeyDrvTest(void);

#endif
